//
//  JCTextView.m
//  JCUIKit
//
//  Created by ChenJianjun on 2017/1/12.
//  Copyright © 2017年 Boych<https://github.com/Boych>. All rights reserved.
//

#import "JCTextView.h"

@interface JCTextView ()

@property (nonatomic, strong) NSMutableParagraphStyle *paragraphStyle;
@property (nonatomic, assign) CGFloat maxHeight;
@property (nonatomic, assign) CGFloat minHeight;
@property (nonatomic, assign) CGFloat currentHeight;

@end

@implementation JCTextView

#pragma mark - Life cycle

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self initilize];
}

- (void)initilize {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textDidChangeNotification:)
                                                 name:UITextViewTextDidChangeNotification
                                               object:nil];
    _placeholderColor = [UIColor lightGrayColor];
    self.font = [UIFont systemFontOfSize:15.f];

}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self initilize];
    }
    return self;
}

#pragma mark - Setter

- (void)setPlaceholder:(NSString *)placeholder
{
    if ([placeholder isEqualToString:_placeholder]) {
        return;
    }
    _placeholder = placeholder;
    
    [self setNeedsDisplay];
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
    if ([placeholderColor isEqual:_placeholderColor]) {
        return;
    }
    _placeholderColor = placeholderColor;
    [self setNeedsDisplay];
}

- (void)setMaxVisibleNumberOfLines:(NSInteger)maxVisibleNumberOfLines
{
    if (maxVisibleNumberOfLines == _maxVisibleNumberOfLines) {
        return;
    }
    _maxVisibleNumberOfLines = maxVisibleNumberOfLines;
    UIEdgeInsets insets = self.contentInset;
    _maxHeight = _maxVisibleNumberOfLines * (self.font.pointSize + 2 + 2) - 2 + insets.top + insets.bottom;
}

#pragma mark - Private

- (void)textDidChangeNotification:(NSNotification *)notification
{
    if (self.textChangedBlock) {
        self.textChangedBlock();
    }
    [self setNeedsDisplay];
}

- (NSMutableParagraphStyle *)paragraphStyle
{
    if (!_paragraphStyle) {
        _paragraphStyle = [NSMutableParagraphStyle new];
    }
    _paragraphStyle.alignment = self.textAlignment;
    _paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    return _paragraphStyle;
}

- (void)resizeViewHeight
{
    if (self.maxVisibleNumberOfLines <= 1) {
        return;
    }
    if (self.minHeight == 0) {
        self.currentHeight = CGRectGetHeight(self.bounds);
        self.minHeight = self.currentHeight;
    }
    
    CGSize size = [self sizeThatFits:self.bounds.size];
    CGFloat height = ceilf(size.height);
    if (height < self.minHeight) {
        height = self.minHeight;
    } else if (height > self.maxHeight) {
        height = self.maxHeight;
    }
    if (height == self.currentHeight) {
        return;
    }
    self.currentHeight = height;
    if (self.heightChangedBlock) {
        self.heightChangedBlock(self.currentHeight);
    }
}

#pragma mark - UIViewRendering

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    if (![self hasText] && self.placeholder && self.placeholderColor) {
        UIEdgeInsets insets = self.contentInset;
        CGFloat fontSize = self.font.pointSize;
        CGRect placeholderFrame = CGRectMake(8 + insets.left, fontSize/2.f + insets.top, CGRectGetWidth(rect) - 16 - insets.left - insets.right, CGRectGetHeight(rect) - fontSize - insets.top - insets.bottom);
        [self.placeholder drawInRect:placeholderFrame
                      withAttributes:@{NSFontAttributeName: self.font,
                                       NSForegroundColorAttributeName: self.placeholderColor,
                                       NSParagraphStyleAttributeName: self.paragraphStyle}];
    }
}

- (void)setNeedsDisplay
{
    [super setNeedsDisplay];
    [self resizeViewHeight];
}

@end
